package Learn;

public class sort {
    public static void main(String[] args) {
        //冒泡排序
        int[] arr1 = {2, 3, 5, 1, 4};
        for (int i = 0; i < arr1.length - 1; i++) {
            for (int j = 0; j < arr1.length - 1 - i; j++) {
                if (arr1[j] > arr1[j + 1]) {
                    int tmp = arr1[j];
                    arr1[j] = arr1[j + 1];
                    arr1[j + 1] = tmp;
                }
            }
        }
        sortPrint(arr1);

        //选择排序
        //从前到后每一个索引与它后面的一个一个比较小的放前面 大的放后面
        int[] arr = {2, 3, 5, 1, 4};
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] > arr[j]) {
                    int tmp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = tmp;
                }
            }
        }
        sortPrint(arr);



    }

    private static void sortPrint(int[] arr1) {
        for (int i = 0; i < arr1.length; i++) {
            System.out.println(arr1[i]);
        }
    }
}
